package com.example.meituan.activity.user.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.meituan.R;
import com.example.meituan.activity.bean.UserAddressBean;
import com.example.meituan.activity.user.OnItemClickType;
import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder>{

    private List<UserAddressBean> addressList;
    private static OnItemClickType listener;//点击样本的不同
    public AddressAdapter(List<UserAddressBean> addressList, OnItemClickType listener) {
        this.addressList=addressList;
        this.listener=listener;
    }
    @NonNull
    @Override
    public AddressAdapter.AddressViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.address_list, parent, false);
        return new AddressAdapter.AddressViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AddressAdapter.AddressViewHolder holder, int position) {
        UserAddressBean addressBean = addressList.get(position);
        holder.bind(addressBean);
    }

    @Override
    public int getItemCount() {
        return addressList.size();
    }

    public static class AddressViewHolder extends RecyclerView.ViewHolder {
        TextView addressFront,addressBack,nameAndphone;
        ImageView update;
        CheckBox checkBox;
        public AddressViewHolder(@NonNull View itemView) {
            super(itemView);
            addressFront=itemView.findViewById(R.id.address_title);
            addressBack=itemView.findViewById(R.id.address_detail_my);
            nameAndphone=itemView.findViewById(R.id.contact_name);
            update=itemView.findViewById(R.id.user_address_update);
            checkBox=itemView.findViewById(R.id.user_address_check);
        }

        public void bind(UserAddressBean userAddressBean) {
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(userAddressBean);
                }
            });
            String address=userAddressBean.getS_address();//"浙江省 宁波市 鄞州区 首南街道-万里路1号浙江万里学院钱湖校区
            String[] parts = address.split("-", 2);
            if (parts.length>1){
                String partBeforeDash = parts[0].trim();
                String partAfterDash = parts[1].trim();
                addressFront.setText(partBeforeDash);
                addressBack.setText(partAfterDash);
            }else {
                addressFront.setText("-");
                addressBack.setText("-");
            }
            nameAndphone.setText(userAddressBean.getS_name()+" "+userAddressBean.getS_phone());
            checkBox.setOnCheckedChangeListener(null); // 防止无限递归

            checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (listener != null) {
                    listener.ifChecked(userAddressBean, isChecked);
                }
            });
            update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemUpder(userAddressBean);
                    }
                }
            });
        }
    }
}
